package nveces.jspwiki.plugins.svg;

import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_ALT;
import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_BORDER;
import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_CLASS;
import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_HEIGHT;
import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_STYLE;
import static nveces.jspwiki.plugins.svg.MagicNamesPluginSvg.PARAM_WIDTH;

import java.util.Map;

/**
 * $Id: HtmlTagCreator.java 10 2011-02-26 20:48:09Z nveces@gmail.com $
 * @author nveces
 *
 */
@SuppressWarnings("unchecked")
public class HtmlTagCreator {

    /**
     * Factory Instance
     */
    private static HtmlTagCreator _instance = null;

    static {
        createInstance();
    } // end static

    public static HtmlTagCreator _() {
        return _instance;
    }

    private static void createInstance() {
        _instance = new HtmlTagCreator();
    }

    protected HtmlTagCreator() {
    }

    public StringBuffer createLink(String target, String href, String label) {
        StringBuffer sb = new StringBuffer();
        sb.append("<a href=\"");
        sb.append(href);
        sb.append("\" ");
        if (target != null && !target.equalsIgnoreCase("")) {
            sb.append("target=\"");
            sb.append(target);
            sb.append("\"");
        }
        sb.append(">");
        sb.append(label);
        sb.append("</a>");
        return sb;
    }

    public StringBuffer createImg(Map map, String imgsrc) {
        StringBuffer sb = new StringBuffer();
        String alt = (String) map.get(PARAM_ALT);
        String border = (String) map.get(PARAM_BORDER);
        sb.append("<img src=\"");
        sb.append(imgsrc);
        sb.append("\"");
        //
        sb.append(this.addAttributes(map));
        if (alt != null) {
            sb.append(" alt=\"");
            sb.append(alt);
            sb.append("\"");
        }
        if (border != null) {
            sb.append(" border=\"");
            sb.append(border);
            sb.append("\"");
        }
        sb.append(" />");
        return sb;
    }


    public StringBuffer createObject(Map map, String src) {
        StringBuffer sb = new StringBuffer();
        sb.append("<!--[if IE]><embed src=\"");
        sb.append(src);
        sb.append("\"");
        sb.append(this.addAttributes(map));
        sb.append("><![endif]-->");
        sb.append("<object data=\"");
        sb.append(src);
        sb.append("\" type=\"image/svg+xml\">");
        sb.append("</object>");
        return sb;
    }

    private String addAttributes(Map map){
        String height = (String) map.get(PARAM_HEIGHT);
        String width = (String) map.get(PARAM_WIDTH);
        String style = (String) map.get(PARAM_STYLE);
        String styleClass = (String) map.get(PARAM_CLASS);
        StringBuffer sb = new StringBuffer();
        if (height != null) {
            sb.append(" height=\"");
            sb.append(height);
            sb.append("\"");
        }
        if (width != null) {
            sb.append(" width=\"");
            sb.append(width);
            sb.append("\"");
        }
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        if (styleClass != null) {
            sb.append(" class=\"");
            sb.append(styleClass);
            sb.append("\"");
        }
        return sb.toString();
    }

}
